package org.nobody.mallb2c.mallcoupon.service.impl;

import org.nobody.common.to.MemberPrice;
import org.nobody.common.to.SkuReductionTo;
import org.nobody.mallb2c.mallcoupon.entity.MemberPriceEntity;
import org.nobody.mallb2c.mallcoupon.entity.SkuLadderEntity;
import org.nobody.mallb2c.mallcoupon.service.MemberPriceService;
import org.nobody.mallb2c.mallcoupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nobody.common.utils.PageUtils;
import org.nobody.common.utils.Query;

import org.nobody.mallb2c.mallcoupon.dao.SkuFullReductionDao;
import org.nobody.mallb2c.mallcoupon.entity.SkuFullReductionEntity;
import org.nobody.mallb2c.mallcoupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
  @Autowired
  SkuLadderService skuLadderService;

  @Autowired
  MemberPriceService memberPriceService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<SkuFullReductionEntity> page = this.page(
      new Query<SkuFullReductionEntity>().getPage(params),
      new QueryWrapper<SkuFullReductionEntity>()
    );

    return new PageUtils(page);
  }

  @Override
  public void saveSkuReduction(SkuReductionTo reductionTo) {
    //1、sms_sku_ladder
    SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
    skuLadderEntity.setSkuId(reductionTo.getSkuId());
    skuLadderEntity.setFullCount(reductionTo.getFullCount()); //满几件
    skuLadderEntity.setDiscount(reductionTo.getDiscount()); //打几折
    skuLadderEntity.setAddOther(reductionTo.getCountStatus()); //是否参与其它优惠
    if(skuLadderEntity.getFullCount()>0) skuLadderService.save(skuLadderEntity);

    //2、sms_sku_full_reduction
    SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
    BeanUtils.copyProperties(reductionTo, skuFullReductionEntity);
    skuFullReductionEntity.setAddOther(reductionTo.getPriceStatus()); //是否参与其它优惠
    if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) this.save(skuFullReductionEntity);

    //3、sms_member_price
    List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
    List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
      MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
      memberPriceEntity.setSkuId(reductionTo.getSkuId());
      memberPriceEntity.setMemberLevelId(item.getId());
      memberPriceEntity.setMemberLevelName(item.getName());
      memberPriceEntity.setMemberPrice(item.getPrice());
      memberPriceEntity.setAddOther(1);
      return memberPriceEntity;
    }).filter(item-> item.getMemberPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
    memberPriceService.saveBatch(collect);
  }

}
